/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.controller;

import com.dao.EntityDAO;
import com.dao.UserDAO;
import com.dao.TypeUserDAO;
import com.entidad.TypeUser;
import com.entidad.User;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Abner Bastarrachea
 */
@WebServlet(name = "UserController", urlPatterns = {"/UserController"})
public class UserController extends HttpServlet {
    UserDAO uDao = new UserDAO();
    EntityDAO eDao = new EntityDAO();
    TypeUserDAO tuDao = new TypeUserDAO();
    
    @Override
    public void destroy() {
        uDao.closeEntityManager();
        eDao.closeEntityManager();
        tuDao.closeEntityManager();
    }

    @Override
    public void init() throws ServletException {
        uDao = new UserDAO();
        eDao = new EntityDAO();
        tuDao = new TypeUserDAO();
    }
    
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        String option = request.getParameter("action");
        if(option.equalsIgnoreCase("insertar")){
            request.setAttribute("listEntity", eDao.getListEntity());
            request.setAttribute("listTypeUser", tuDao.getListTypeUser());
            RequestDispatcher rd = request.getRequestDispatcher("/User/addUser.jsp");
            rd.forward(request, response);
        } else if (option.equalsIgnoreCase("guardar")){
            String user = request.getParameter("txtUser");
            String password = request.getParameter("txtPassword");
            Integer fkEntity = Integer.parseInt(request.getParameter("cboEntity"));
            Integer fkTypeUser = Integer.parseInt(request.getParameter("cboTypeUser"));
            User u = new User();
            u.setUser(user);
            u.setPassword(password);
            u.setFkEntity(fkEntity);
            u.setFkTypeUser(fkTypeUser);

            if (uDao.insertarUser(u)){
                response.sendRedirect("exito.jsp");
            } else {
                response.sendRedirect("error.jsp");
            }
        } else if (option.equalsIgnoreCase("listado")){
            request.setAttribute("listUser", uDao.getListUser());
            RequestDispatcher rd = request.getRequestDispatcher("/User/listUser.jsp");
            rd.forward(request, response);
        } else if (option.equalsIgnoreCase("modificar")){
            Integer id = Integer.parseInt(request.getParameter("codigo"));
            User u = uDao.buscarUser(id);
            if (u == null)
                response.sendRedirect("found.jsp");
            else{
                request.setAttribute("user", u);
                RequestDispatcher rd = request.getRequestDispatcher("/User/editUser.jsp");
                rd.forward(request, response);
            }
        } else if (option.equalsIgnoreCase("eliminar")){
            Integer id = Integer.parseInt(request.getParameter("codigo"));
           
            if (uDao.eliminarUser(id)){
                response.sendRedirect("exito.jsp");
            } else {
                response.sendRedirect("error.jsp");
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
